<html>
<head>
  <script src="OLLoader.js"></script>
  <script type="text/javascript">
    var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
    var map; 
    var a;
    
    function test_Events_constructor (t) {
        t.plan(4);

        var mapDiv = OpenLayers.Util.getElement('map');
        var obj = {result: 0};

        events = new OpenLayers.Events(obj, mapDiv);
        t.ok( events instanceof OpenLayers.Events, "new OpenLayers.Control returns object" );
        t.ok(events.object ==obj, " 'object' property correctly set");
        t.ok(events.element == mapDiv, " 'element' property correctly set");
        events.destroy();

        // default/nulls
        events = new OpenLayers.Events(null, null, null);
        t.ok( events.listeners != null,
              "init of Events with null object/element/eventTypes still creates listeners array" );
        events.destroy();
    }

    function test_Events_register(t){
        t.plan(4);

        var ev = {
            'object': {},
            'extensionCount': {
                'listenerA': 0,
                'listenerB': 0
            },
            'listeners': {
                'listenerA': { 
                    'push': function(options){
                                gObjA = options.obj;
                                gFuncA = options.func;
                            }
                },
                'listenerB': { 
                    'push': function(options){
                                gObjB = options.obj;
                                gFuncB = options.func;
                            }
                }
            }
        };

        var type = null;
        var object = null;
        var func = null;

      //func null
        gObjA = null; gFuncA = null; gObjB = null; gFuncB = null;
        OpenLayers.Events.prototype.register.apply(ev, [type, object, func]);
        t.ok((gObjA == null) && (gFuncA == null) && 
             (gObjB == null) && (gFuncB == null), "no push called func null"); 

      //valid func, type not in ev.eventTypes
        func = function() {};
        gObjA = null; gFuncA = null; gObjB = null; gFuncB = null;
        OpenLayers.Events.prototype.register.apply(ev, [type, object, func]);
        t.ok((gObjA == null) && (gFuncA == null) && 
             (gObjB == null) && (gFuncB == null), "no push called func null"); 

      //valid func, type in ev.eventTypes, null obj
        type = 'listenerA';
        gObjA = null; gFuncA = null; gObjB = null; gFuncB = null;
        OpenLayers.Events.prototype.register.apply(ev, [type, object, func]);
        t.ok((gObjA == ev.object) && (gFuncA == func) && 
             (gObjB == null) && (gFuncB == null), "push called on listenerA's mock array when type passed in 'listenerA'. events.object taken since obj is null."); 
        
      //valid func, type in ev.eventTypes, valid obj
        type = 'listenerB';
        object = {};
        gObjA = null; gFuncA = null; gObjB = null; gFuncB = null;
        OpenLayers.Events.prototype.register.apply(ev, [type, object, func]);
        t.ok((gObjA == null) && (gFuncA == null) && 
             (gObjB == object) && (gFuncB == func), "push called on listenerB's mock array when type passed in 'listenerB'."); 
        
    }

    function test_Events_register_unregister(t) {
 
        t.plan(20);
 
        var mapDiv = OpenLayers.Util.getElement('map');
        var obj = {result: 0};
        
        events = new OpenLayers.Events(obj, mapDiv);
        
        var func = function () { this.result++ }
        events.register( "doThingA", obj, func );

        var listenerList = events.listeners["doThingA"];
        t.eq( listenerList.length, 1, "register correctly adds to event.listeners" );
        t.ok( listenerList[0].obj == obj, "obj property correctly registered");
        t.ok( listenerList[0].func == func, "func property correctly registered");

        var func2 = function () { this.result-- }
        events.register( "doThingA", obj, func2 );

        var listenerList = events.listeners["doThingA"];
        t.eq( listenerList.length, 2, "register correctly appends new callback to event.listeners[doThingA]" );
        t.ok( listenerList[1].obj == obj, "obj property correctly registered");
        t.ok( listenerList[1].func == func2, "func property correctly registered");

        var func3 = function () { this.result = this.result + 3; }
        events.register( "doThingA", null, func3 );

        var listenerList = events.listeners["doThingA"];
        t.eq( listenerList.length, 3, "register correctly appends new callback to event.listeners[doThingA] even when obj passed in is null" );
        t.ok( listenerList[2].obj == obj, "obj is correctly set to Events.object default when null is passed in.");
        t.ok( listenerList[2].func == func3, "func property correctly registered");

        events.register( "doThingA", obj, null);

        var listenerList = events.listeners["doThingA"];
        t.eq( listenerList.length, 3, "register correctly does not append null callback to event.listeners[doThingA] even when obj passed in is null" );

        events.register("chicken", obj, func);
        t.eq(events.listeners["chicken"].length, 1, "register() allows listeners for any named event");

        events.unregister("chicken", obj, func);
        t.eq(events.listeners["chicken"].length, 0, "unregistering an event that is not in eventTypes list works")

        events.unregister("doThingA", obj, null);
        var listenerList = events.listeners["doThingA"];
        t.eq( listenerList.length, 3, "trying to unregister a null callback does nothing -- but doesnt crash :-)" );
    
        events.unregister("doThingA", obj, func);
        var found = false;
        for (var i = 0; i < listenerList.length; i++) {
            var listener = listenerList[i];
            if (listener.obj == obj && listener.func == func) {
                found = true;
            }
        }                
        t.ok( (listenerList.length == 2) && !found, "unregister correctly removes callback" );

        events.unregister("doThingA", null, func3);
        var found = false;
        for (var i = 0; i < listenerList.length; i++) {
            var listener = listenerList[i];
            if (listener.obj == obj && listener.func == func) {
                found = true;
            }
        }                
        t.ok( (listenerList.length == 1) && !found, "unregister correctly removes callback when no obj specified" );
        
        var func4 = function () { this.result = "chicken"; }
        events.unregister("doThingA", obj, func4);
        t.ok( (listenerList.length == 1), "unregister does not bomb if you try to remove an unregistered callback" );

        var obj2 = { chicken: 151 };
        events.unregister("doThingA", obj2, func2);
        t.ok( (listenerList.length == 1), "unregister does not bomb or accidntally remove if you try to remove a valid callback on a valid event type, but with the wrong context object" );

        events.unregister("doThingA", obj, null);
        t.ok( (listenerList.length == 1), "unregister does not bomb if you try to remove a null callback" );

        try {
            events.unregister("asdf", obj, func);
            t.ok("unregistering for an event with no registered listeners works");
        } catch (err) {
            t.fail("unregistering for an event with no registered listeners causes trouble: " + err);
        }
        
        events.register("buttonclick", obj, func);
        t.ok(events.extensions.buttonclick, "buttonclick extension registered");

    }

    function test_Events_remove(t) {

        t.plan( 2 );
 
        var mapDiv = OpenLayers.Util.getElement('map');
        var obj = {result: 0};
        
        events = new OpenLayers.Events(obj, mapDiv);
        
        var func = function () { this.result++ }
        var func2 = function () { this.result-- }
        var func3 = function () { this.result = this.result + 3; }

        events.register( "doThingA", obj, func );
        events.register( "doThingA", obj, func2 );
        events.register( "doThingA", null, func3 );

        events.remove("doThingA");

        t.eq( events.listeners["doThingA"].length, 0, "remove() correctly clears the event listeners" );

        events.remove("chicken");
        t.ok( events.listeners["chicken"] == null, "remove on non-enabled event does not break or accidentally enable the event");

    }

    function test_Events_triggerEvent(t) {
    
        t.plan(13);
 
        var mapDiv = OpenLayers.Util.getElement('map');
        var obj = {result: 0};
        
        events = new OpenLayers.Events(obj, mapDiv);
        
        
        var func = function () { this.result++ }
        events.register( "doThingA", obj, func );

        events.triggerEvent("doThingA", {});
        t.eq( obj.result, 1, "result is 1 after we call triggerEvent" );
        events.triggerEvent("doThingA");
        t.eq( obj.result, 2, "result is 2 after we call triggerEvent with no event" );

        var funcB = function() { this.FUNK = "B"; return false; }
        events.register( "doThingA", obj, funcB);

        events.triggerEvent("doThingA");
        t.ok ((obj.result == 3) && (obj.FUNK == "B"), "executing multiple callbacks works")

        var funcZ = function() { this.FUNK = "Z"; }
        events.register( "doThingA", obj, funcZ);

        events.triggerEvent("doThingA");
        t.ok ((obj.result == 4) && (obj.FUNK == "B"), "executing multiple callbacks works, and when one returns false, it stops chain correctly")


        var func2 = function() { this.result = this.result + 10; }
        events.register( "doThingB", null, func2);
        
        events.triggerEvent("doThingB");
        t.eq( obj.result, 14, "passing a null object default gets set correctly");
        
        //no specific t.ok for this one, but if it breaks, you will know it.
        events.triggerEvent("chicken");

        events = new OpenLayers.Events(null, mapDiv);
        
        // a is global variable (context-irrelevant)
        a = 0;
        var func = function () { a = 5; }
        events.register( "doThingC", null, func );
        events.triggerEvent("doThingC");

        t.eq(a, 5, "if Events has no object set and an event is registered also with no object, triggerEvent() calls it without trying to set the context to null");
        
        // trigger events with additional arguments
        events = new OpenLayers.Events();
        var instance = {id: Math.random()};
        var listener = function(obj) {
            t.eq(this.id, instance.id,
                 "listener called with proper scope");
            t.eq(arguments.length, 1,
                 "listener called with a single argument");
            t.eq(typeof arguments, "object",
                 "listener called with an object");
            t.eq(obj.foo, evt.foo,
                 "foo property set on the layer");
        };
        events.register("something", instance, listener);        
        var evt = {
            id: Math.random(),
            "foo": "bar"
        };
        events.triggerEvent("something", evt);
        events.unregister("something", instance, listener);
        
        // test return from triggerEvent
        var listener1 = function() {
            return "foo";
        }
        var listener2 = function() {
            return false;
        }
        var listener3 = function() {
            t.fail("never call me again!");
        }
        events.register("something", instance, listener1);
        var ret = events.triggerEvent("something", evt);
        t.eq(ret, "foo", "correct return from single listener");
        
        events.register("something", instance, listener2);
        ret = events.triggerEvent("something", evt);
        t.eq(ret, false, "correct return for two listeners");
        
        events.register("something", instance, listener3);
        ret = events.triggerEvent("something", evt);
        t.eq(ret, false, "correct return for three listeners where second cancels");
        
        events.unregister("something", instance, listener1);
        events.unregister("something", instance, listener2);
        events.unregister("something", instance, listener3);
    }

    function test_Events_handleBrowserEvent(t) {
        t.plan(2);
        var events = new OpenLayers.Events({}, null);
        events.on({'sometouchevent': function() {}});

        // this test verifies that when handling a touch event we correctly
        // set clientX and clientY in the event object 
        var evt = {type: 'sometouchevent',
                   touches: [{clientX: 1, clientY: 1}, {clientX: 2, clientY: 2}]
                  };
        events.handleBrowserEvent(evt);
        t.eq(evt.clientX, 1.5, "evt.clientX value is correct");
        t.eq(evt.clientY, 1.5, "evt.clientY value is correct");
    }
    
    function test_Events_attachToElement(t) {
        t.plan(3);
        var events = new OpenLayers.Events({}, null);
        var element = document.createElement("div");
        events.attachToElement(element);
        t.ok(events.eventHandler, "eventHandler method bound");
        t.ok(events.clearMouseListener, "clearMouseListener method bound");
        t.ok(events.element === element, "element set");
    }

    function test_Events_destroy (t) {
        t.plan(2);

        var div   = OpenLayers.Util.getElement('test');
        var obj   = {};
        var events = new OpenLayers.Events(obj, div);

        // +1 because of blocking dragstart in attachToElement()
        t.eq(OpenLayers.Event.observers[div._eventCacheID].length,
             OpenLayers.Events.prototype.BROWSER_EVENTS.length + 1,
             "construction creates new arrayin hash, registers appropriate events");
             
        events.destroy();
        events = null;
        t.eq(OpenLayers.Event.observers["test"], null,
             "destruction removes the event observer from hash");
    }
    
    function test_Event(t) {
        t.plan(24);
        
        var div   = OpenLayers.Util.getElement('test');
        var name = "mouseover";
        var func = function() {};

      //1st elem 1st listener        
        OpenLayers.Event.observe(div, name, func);

        var cacheID = div._eventCacheID;
        t.ok(cacheID, "element given new cache id");
        
        var elementObservers = OpenLayers.Event.observers[cacheID];
        
        t.ok(elementObservers, "new cache bucket made for event");
        t.eq(elementObservers.length, 1, "one listener registered");        
        
        var listener = elementObservers[0];
        
        t.ok(listener.element == div, "element registered");
        t.eq(listener.name, name, "name registered");
        t.ok(listener.observer == func, "function registered");
        t.eq(listener.useCapture, false, "useCapture defaults to false");

      //1st elem 2nd listener        
        name = "mouseout";
        var newFunc = function() {};
      
        OpenLayers.Event.observe(div, name, newFunc, true);
        var newCacheID = div._eventCacheID;
        t.eq(newCacheID, cacheID, "element's cache id not overridden");
        
        t.eq(elementObservers.length, 2, "listener added to existing bucket");        
        
        var listener = elementObservers[1];
        
        t.ok(listener.element == div, "element registered");
        t.eq(listener.name, name, "name registered");
        t.ok(listener.observer == newFunc, "function registered");
        t.eq(listener.useCapture, true, "useCapture correctly registered");

      //2st elem 1st listener        
        div = OpenLayers.Util.getElement('test2'); 
        OpenLayers.Event.observe(div, name, func);

        var cacheID = div._eventCacheID;
        t.ok(cacheID, "new element given new cache id");
        t.ok(cacheID != newCacheID, "new cache id is unique");
        
        elementObservers = OpenLayers.Event.observers[cacheID];
        
        t.ok(elementObservers, "new cache bucket made for event");
        t.eq(elementObservers.length, 1, "one listener registered");        
        
        var listener = elementObservers[0];
        
        t.ok(listener.element == div, "element registered");
        t.eq(listener.name, name, "name registered");
        t.ok(listener.observer == func, "function registered");
        t.eq(listener.useCapture, false, "useCapture defaults to false");

      //stopObservingElement by element
        OpenLayers.Event.stopObservingElement(div);
        elementObservers = OpenLayers.Event.observers[cacheID];
        t.ok(elementObservers == null, "stopObservingElement by elem works");

      //stopObservingElement by id
        OpenLayers.Event.stopObservingElement("test");
        elementObservers = OpenLayers.Event.observers[newCacheID];
        t.ok(elementObservers == null, "stopObservingElement by id works");


      //unloadCache by element
        OpenLayers.Event.observe(div, name, func);
        
        OpenLayers.Event.unloadCache();

        elementObservers = OpenLayers.Event.observers[cacheID];
        t.ok(elementObservers == null, "stopObservingElement by elem works");

        
    }

  </script>
</head>
<body>
    <div id="map" style="width: 1024px; height: 512px;"/>
    <div id="test"></div>
    <div id="test2"></div>
</body>
</html>
